﻿#include "Update.h"
#include "ui_Update.h"

Update::Update(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    m_radarip = "";
    ui.radioButton_4->setVisible(false);
    ui.radioButton_5->setVisible(false);
    ui.checkBox_5->setVisible(false);
    ui.pushButton_13->setVisible(false);
    ui.label_15->setVisible(false);
    ui.lineEdit_14->setVisible(false);
    if(TYPE){
        ui.checkBox_5->setVisible(true);
        ui.pushButton_13->setVisible(true);
        ui.checkBox_6->setVisible(true);
        ui.pushButton_16->setVisible(true);
        ui.lineEdit_19->setVisible(true);
        ui.label_20->setVisible(true);
        ui.lineEdit_14->setVisible(true);
        ui.label_15->setVisible(true);
    }else{
        ui.checkBox_6->setVisible(false);
        ui.pushButton_16->setVisible(false);
        ui.lineEdit_19->setVisible(false);
        ui.label_20->setVisible(false);
    }
    m_socket = new QTcpSocket;
    connect(m_socket, SIGNAL(connected()), this, SLOT(Slot_connected()));
    connect(m_socket, SIGNAL(disconnected()), this, SLOT(Slot_disconnected()));
    m_readb = new QTimer;
    connect(m_readb, SIGNAL(timeout()), this, SLOT(Slot_checkversion()));
    //loadStyleSheet(":/Update/Resources/blacknew.qss");

    this->setWindowTitle("雷视一体机升级软件工厂版V1.0.3");

    this->setWindowIcon(QIcon(":/folder_sent_mail.ico"));
    QRegExp rx("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$");
    QRegExpValidator *validator = new QRegExpValidator(rx);
    ui.lineEdit_6->setValidator(validator);
    QRegExp portrx("^([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-5]{2}[0-3][0-5])$");
    QRegExpValidator *validatorport = new QRegExpValidator(portrx);
    ui.lineEdit_7->setValidator(validatorport);
    ui.comboBox->addItem("转接板");
    ui.comboBox->addItem("远程板");
    ui.comboBox->addItem("中程板");
    ui.comboBox->addItem("近程板");
    //loadStyleSheet(":/Update/Resources/qss/blue.css");
    m_thread = new tcpThread;
    font = QFont("KaiTi", 14, 2);
    ui.textEdit->setFont(font);
    ui.textEdit->setReadOnly(true);
    //    connect(ui.pushButton,SIGNAL(clicked()),this,SLOT(Slot_Connect()));
    //    connect(ui.pushButton_2,SIGNAL(clicked()),this,SLOT(Slot_DisConnect()));
    connect(ui.pushButton_3,SIGNAL(clicked()),this,SLOT(Slot_Choosefile()));
    connect(ui.pushButton_4,SIGNAL(clicked()),this,SLOT(Slot_LockUpdatefile()));
    connect(ui.pushButton_5,SIGNAL(clicked()),this,SLOT(Slot_StartUpdate()));
    connect(ui.pushButton_6,SIGNAL(clicked()),this,SLOT(Slot_StopUpdate()));
    connect(ui.pushButton_7,SIGNAL(clicked()),this,SLOT(Slot_Version()));
    connect(ui.pushButton_8,SIGNAL(clicked()),this,SLOT(Slot_ClearLog()));
    connect(ui.pushButton_9,SIGNAL(clicked()),this,SLOT(Slot_savefiledir()));
    connect(ui.pushButton_10,SIGNAL(clicked()),this,SLOT(Slot_h7tsmfile()));
    connect(ui.pushButton_11,SIGNAL(clicked()),this,SLOT(Slot_lrrtsmfile()));
    connect(ui.pushButton_12,SIGNAL(clicked()),this,SLOT(Slot_mrrtsmfile()));
    connect(ui.pushButton_13,SIGNAL(clicked()),this,SLOT(Slot_srrtsmfile()));
    connect(ui.checkBox,SIGNAL(stateChanged(int)),this,SLOT(Slot_LockMsg(int)));
    connect(ui.radioButton_3, SIGNAL(clicked()), this, SLOT(choose50()));
    connect(ui.radioButton_4, SIGNAL(clicked()), this, SLOT(choose35()));
    connect(ui.radioButton_5, SIGNAL(clicked()), this, SLOT(choose30()));
    connect(this, SIGNAL(connectRadar(QString, quint16, QString, QString, int, bool)), m_thread, SLOT(slotConnectRadar(QString, quint16, QString, QString, int, bool)));
    connect(this, SIGNAL(sendTsm(const QByteArray,unsigned int)), m_thread, SLOT(Slot_revTsm(const QByteArray,unsigned int)));
    connect(this, SIGNAL(disconnectRadar()), m_thread, SLOT(slotDisConnectRadar()));
    connect(m_thread, SIGNAL(UpdateAnswer(int)), this, SLOT(Slot_UpdateAnswer(int)));
    connect(this, SIGNAL(updatecmd(int)), m_thread, SLOT(Slot_Updatecmd(int)));
    //connect(m_thread, SIGNAL(Reloadprogress(int)), this, SLOT(Slot_Reloadprogress(int)));
    connect(m_thread, SIGNAL(sendversionmsg(const QByteArray&)), this, SLOT(Slot_CheckH7version(const QByteArray&)));
    connect(m_thread, SIGNAL(sethardidover()), this, SLOT(Slot_sethardidover()));
    connect(m_thread, SIGNAL(sendRadarmsg(QString,int)), this, SLOT(Slot_Revradarmsg(QString,int)));
    connect(this, SIGNAL(sethardid(QString)), m_thread, SLOT(Slot_sethardid(QString)));
    m_anstimer = new QTimer;
    m_ganstimer = new QTimer;
    m_setid = new QTimer;
    m_recontimer = new QTimer;
    m_checkversiontimer = new QTimer;
    connect(m_checkversiontimer, SIGNAL(timeout()), this, SLOT(Slot_checkversiontimeout()));
    connect(m_recontimer, SIGNAL(timeout()), this, SLOT(Slot_reconnect()));
    connect(m_setid, SIGNAL(timeout()), this, SLOT(Slot_setidout()));
    connect(m_ganstimer, SIGNAL(timeout()), this, SLOT(Slot_Gupdateans()));
    connect(m_anstimer, SIGNAL(timeout()), this, SLOT(Slot_Waitforans()));
    connect(m_thread, SIGNAL(cmdtimeout(int)), this, SLOT(Slot_Showtimeout(int)));
    m_envirgroup = new QButtonGroup;
    m_envirgroup->addButton(ui.radioButton, 0);
    m_envirgroup->addButton(ui.radioButton_2, 1);
    //连接槽函数
    connect(m_envirgroup, SIGNAL(buttonToggled(int, bool)), this, SLOT(Slot_EnvironmentChange(int, bool)));
    connect(this, SIGNAL(openbootkey()), m_thread, SLOT(Slot_openbootkey()));
    connect(this, SIGNAL(closebootkey()), m_thread, SLOT(Slot_closebootkey()));
    QSettings setting("FileAddress.ini", QSettings::IniFormat);
    setting.setIniCodec(QTextCodec::codecForName("GB2312"));
    ui.lineEdit_6->setText(setting.value("File/IP").toString());
    //ui.lineEdit_7->setText(setting.value("File/PORT").toString());
    ui.pushButton_2->setVisible(false);
    ui.pushButton_7->setVisible(false);
    ui.groupBox_9->setVisible(false);
    //ui.groupBox_5->setVisible(false);
    //ui.checkBox_6->setVisible(true);
    ui.label_10->setVisible(false);
    ui.label_6->setVisible(false);
    ui.label_7->setVisible(false);
    ui.label_8->setVisible(false);
    ui.lineEdit_9->setVisible(false);
    ui.label_11->setVisible(false);
    ui.lineEdit_10->setVisible(false);
    ui.lineEdit_7->setVisible(false);
    ui.lineEdit_8->setVisible(false);
    //ui.checkBox_2->setVisible(false);
    ui.comboBox->setVisible(false);
    ui.pushButton_3->setVisible(false);
    ui.pushButton->setVisible(false);
    ui.pushButton_2->setVisible(false);
    ui.label_9->setVisible(false);
    ui.pushButton_4->setVisible(false);
    ui.groupBox_5->setVisible(false);
    //ui.pushButton_12->setVisible(false);
    //ui.label_14->setVisible(false);
    //ui.lineEdit_12->setVisible(false);
    for (int i = 0; i < 5; i++){
        m_choosefile[i] = false;
    }

    ui.label_18->setVisible(false);
    ui.label_19->setVisible(false);
    ui.lineEdit_17->setVisible(false);
    ui.lineEdit_18->setVisible(false);
    ui.pushButton_10->setVisible(false);
    ui.pushButton_14->setVisible(false);



    // Create a thread for ADB worker
    adbWorker = new ADBWorker;
    adbThread = new QThread;
    connect(adbThread, &QThread::finished, this, &Update::Slot_threadfinished,Qt::QueuedConnection);
    // Move the ADB worker object to the ADB thread
//    adbWorker->moveToThread(adbThread);

//    // Connect signals to slots in the ADB thread
//   QObject::connect(&adbThread, &QThread::started, &adbWorker, &ADBWorker::checkdevice);
    //connect(&adbThread, &QThread::finished, &adbThread, &QThread::deleteLater);
//    // Start the ADB thread
//    adbThread.start();
//    adbThread.quit();
//    adbThread.wait();
//    adbThread.start();
    return;
}

Update::~Update()
{
    disconnect(adbThread, &QThread::finished, this, &Update::Slot_threadfinished);
    m_anstimer->stop();
    m_ganstimer->stop();
    m_setid->stop();
    m_readb->stop();
    adbThread->quit();
    adbThread->wait();

    delete m_readb;
    delete m_socket;
    delete m_anstimer;
    delete m_ganstimer;
    delete m_setid;
    delete m_envirgroup;
    delete adbThread;
    delete adbWorker;
}

void Update::loadStyleSheet(const QString &sheetName){
    QFile file(sheetName);
    file.open(QFile::ReadOnly);
    if (file.isOpen())
    {
        QString qss = QLatin1String(file.readAll());
        this->setStyleSheet(qss);
        file.close();
    }
    else
    {
        QMessageBox::information(this, "tip", "cannot find qss file");
    }
}

void Update::Slot_Connect(){
}

void Update::Slot_DisConnect(){ 
}

void Update::Slot_Choosefile(){
}

void Update::Slot_CheckH7version(const QByteArray& ary){
    qDebug() << ary;
}

//开始升级
void Update::Slot_StartUpdate(){

    if(adbThread->isRunning()){
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("ADB线程正在工作,请等待结束");
        return;
    }

    if (ui.radioButton_3->isChecked())
    {
        ui.textEdit->clear();
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("雷达类型为远中近");
    }else if (ui.radioButton_4->isChecked())
    {
        ui.textEdit->clear();
        ui.textEdit->setTextColor(Qt::red);
        //ui.textEdit->append("雷达类型为35");
    }else if (ui.radioButton_5->isChecked())
    {
        ui.textEdit->clear();
        ui.textEdit->setTextColor(Qt::red);
        //ui.textEdit->append("雷达类型为30");
    }
    else
    {
        ui.textEdit->clear();
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("未选择雷达类型，无法开始升级");
        return;
    }
    if (ui.lineEdit_6->text().size() < 1 || ui.lineEdit_7->text().size() < 1)
    {
        ui.textEdit->clear();
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("IP地址或端口号未填写");
        return;
    }
    if (!m_choosefile[0] && ui.checkBox_6->isChecked()){
        ui.textEdit->clear();
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("摄像头未选择升级固件");
        return;
    }
    if (!m_choosefile[4] && ui.checkBox_2->isChecked()){
        ui.textEdit->clear();
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("转接板未选择升级固件");
        return;
    }
    if (!m_choosefile[1] && ui.checkBox_3->isChecked()){
        ui.textEdit->clear();
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("远程板未选择升级固件");
        return;
    }
    if (!m_choosefile[2] && ui.checkBox_4->isChecked()){
        ui.textEdit->clear();
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("中程板未选择升级固件");
        return;
    }
    if (!m_choosefile[3] && ui.checkBox_5->isChecked()){
        ui.textEdit->clear();
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("近程板未选择升级固件");
        return;
    }
    m_radarip = ui.lineEdit_6->text();
    //ui.pushButton_5->setText("升级已开始");
    ui.progressBar->setValue(0);
    updatenum = 0;
    updatednum = 0;
    for (int i = 0; i < 5; i++)
    {
        updatename[i] = false;
    }

    if (ui.radioButton_3->isChecked())
    {
        //转接板
        if (ui.checkBox_2->isChecked())
        {
            updatename[0] = true;
            updatenum++;
        }
        //摄像头
        if (ui.checkBox_6->isChecked())
        {
            updatename[4] = true;
            updatenum++;
        }
        //远程板
        if (ui.checkBox_3->isChecked())
        {
            updatename[1] = true;
            updatenum++;
        }
        //中程板
        if (ui.checkBox_4->isChecked())
        {
            updatename[2] = true;
            updatenum++;
        }
        //近程板
        if (ui.checkBox_5->isChecked())
        {
            updatename[3] = true;
            updatenum++;
        }
    }

//    else if (ui.radioButton_4->isChecked())
//    {
//        if (ui.checkBox_2->isChecked())
//        {
//            updatename[0] = true;
//            updatenum++;
//        }
//        if (ui.checkBox_3->isChecked())
//        {
//            updatename[1] = true;
//            updatenum++;
//        }
//        if (ui.checkBox_5->isChecked())
//        {
//            updatename[3] = true;
//            updatenum++;
//        }
//    }
//    else if (ui.radioButton_5->isChecked())
//    {
//        if (ui.checkBox_2->isChecked())
//        {
//            updatename[0] = true;
//            updatenum++;
//        }
//        if (ui.checkBox_4->isChecked())
//        {
//            updatename[2] = true;
//            updatenum++;
//        }
//        if (ui.checkBox_5->isChecked())
//        {
//            updatename[3] = true;
//            updatenum++;
//        }
//    }


    //ui.textEdit->clear();

    ui.textEdit->setTextColor(Qt::black);
    ui.textEdit->append("连接设备，查询硬件特征号");
    m_serialnum = "";
    m_macaddress = "";
    m_bootversion = "CSR_TN_BOOT";
    m_appversion = "";
    m_hardwarenum = "";

    //TCP连接或ADB连接
    if(ui.checkBox_2->isChecked() || ui.checkBox_6->isChecked()){
        if(ui.checkBox_2->isChecked()){
            m_type = true;
            //updatename[0] = false;
        }
        if(ui.checkBox_6->isChecked()){
            m_type = false;
        }
        if(m_type){
            updatename[0] = false;
        }
        adbWorker->moveToThread(adbThread);
        disconnect(adbThread, &QThread::started, adbWorker, &ADBWorker::checkdevice);
        disconnect(adbWorker, &ADBWorker::sendADBmsg, this, &Update::Slot_showADBans);
        connect(adbThread, &QThread::started, adbWorker, &ADBWorker::checkdevice);
        connect(adbWorker, &ADBWorker::sendADBmsg, this, &Update::Slot_showADBans,Qt::QueuedConnection);
        adbThread->start();

    }else{
        emit connectRadar(ui.lineEdit_6->text(), ui.lineEdit_7->text().toUShort(), ui.lineEdit_8->text(), ui.lineEdit_9->text(), 0, ui.radioButton->isChecked());
    }
}


//终止升级
void Update::Slot_StopUpdate(){
    //ui.pushButton_5->setText("开始升级");
    m_ganstimer->stop();
    m_anstimer->stop();
    m_readb->stop();
    emit disconnectRadar();
    ui.textEdit->setTextColor(Qt::red);
    ui.textEdit->append("手动终止升级过程");
    ui.textEdit->append("设备自动断开，升级固件需要重新选择");
    if(adbThread->isRunning()){
        ui.textEdit->append("[上位机]请等待ADB操作终止再进行升级");
        qDebug() << "adbThread";
        adbThread->quit();
        //adbThread->wait();
    }

    //m_choosefile = false;
    saveUpdatelog(false);
}
//锁定升级文件
void Update::Slot_LockUpdatefile(){
    if (ui.pushButton_4->text() == "锁定升级文件")
    {
        ui.pushButton_4->setText("解锁升级文件");
        ui.pushButton_3->setEnabled(false);
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("升级固件已锁定");
    } else
    {
        ui.pushButton_4->setText("锁定升级文件");
        ui.pushButton_3->setEnabled(true);
    }
}
//锁定信息输入
void Update::Slot_LockMsg(int state){
    qDebug() << state;
    if (ui.checkBox->isChecked())
    {
        ui.lineEdit->setEnabled(false);
        ui.lineEdit_2->setEnabled(false);
        ui.lineEdit_3->setEnabled(false);
        ui.lineEdit_4->setEnabled(false);
        ui.lineEdit_5->setEnabled(false);
        ui.lineEdit_6->setEnabled(false);
        ui.lineEdit_7->setEnabled(false);
        ui.lineEdit_8->setEnabled(false);
        ui.lineEdit_9->setEnabled(false);
        ui.radioButton->setEnabled(false);
        ui.radioButton_2->setEnabled(false);
        ui.lineEdit_10->setEnabled(false);
        ui.pushButton_9->setEnabled(false);
        ui.pushButton_4->setEnabled(false);
        ui.pushButton_10->setEnabled(false);
        ui.checkBox_2->setEnabled(false);
        ui.checkBox_6->setEnabled(false);
        ui.pushButton_11->setEnabled(false);
        ui.checkBox_3->setEnabled(false);
        ui.pushButton_12->setEnabled(false);
        ui.checkBox_4->setEnabled(false);
        ui.pushButton_13->setEnabled(false);
        ui.checkBox_5->setEnabled(false);
        ui.lineEdit_15->setEnabled(false);
        ui.lineEdit_16->setEnabled(false);
        ui.groupBox_7->setEnabled(false);
        ui.pushButton_15->setEnabled(false);
        ui.pushButton_16->setEnabled(false);
    }
    else
    {
        ui.pushButton_15->setEnabled(true);
        ui.groupBox_7->setEnabled(true);
        ui.lineEdit_16->setEnabled(true);
        ui.lineEdit_15->setEnabled(true);
        ui.lineEdit->setEnabled(true);
        ui.lineEdit_2->setEnabled(true);
        ui.lineEdit_3->setEnabled(true);
        ui.lineEdit_4->setEnabled(true);
        ui.checkBox_2->setEnabled(true);
        ui.lineEdit_5->setEnabled(true);
        ui.lineEdit_6->setEnabled(true);
        ui.lineEdit_7->setEnabled(true);
        ui.lineEdit_8->setEnabled(true);
        ui.lineEdit_9->setEnabled(true);
        ui.radioButton->setEnabled(true);
        ui.radioButton_2->setEnabled(true);
        ui.lineEdit_10->setEnabled(true);
        ui.pushButton_9->setEnabled(true);
        ui.pushButton_4->setEnabled(true);
        ui.pushButton_10->setEnabled(true);
        ui.checkBox_2->setEnabled(true);
        ui.pushButton_11->setEnabled(true);
        ui.checkBox_3->setEnabled(true);
        ui.pushButton_12->setEnabled(true);
        ui.checkBox_4->setEnabled(true);
        ui.pushButton_13->setEnabled(true);
        ui.pushButton_16->setEnabled(true);
        ui.checkBox_5->setEnabled(true);
        ui.checkBox_6->setEnabled(true);
    }
}
//清空日志
void Update::Slot_ClearLog(){
    ui.textEdit->clear();
}
//版本信息
void Update::Slot_Version(){
    ui.textEdit->setTextColor(Qt::blue);
    ui.textEdit->append("雷视一体机升级软件工厂版V1.0.0 -- 2024.04.30");
}

void Update::Slot_Waitforans(){
    ui.textEdit->setTextColor(Qt::black);
    ui.textEdit->append("[上位机]等待雷达回复...");
}

void Update::Slot_Gupdateans(){
    ui.textEdit->setTextColor(Qt::black);
    ui.textEdit->append("[转接板]正在向芯片下发升级固件...");
}

void Update::Slot_UpdateAnswer(int cmdtype){
    m_anstimer->stop();
    m_ganstimer->stop();
    switch (cmdtype)
    {
    case -1:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("指令发送失败，请检查网络连接");
        //ui.pushButton_5->setText("开始升级");
        break;
    case -2:
//        ui.textEdit->setTextColor(Qt::red);
//        ui.textEdit->append("无设备处在连接状态");
        //ui.pushButton_5->setText("开始升级");
        break;
    case 0:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("设备连接成功,查询雷达信息（序列号,BOOT版本,APP版本,硬件特征号,MAC地址）");
        m_anstimer->start(2000);
        break;
    case 1:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("设备连接超时");
        //ui.pushButton_5->setText("开始升级");
        break;
    case 2:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("设备注销");
        //ui.pushButton_5->setText("开始升级");
        break;
    case 3:
        ui.textEdit->setTextColor(Qt::blue);
        ui.textEdit->append("转接板开始升级，设备重启完成，等待自动连接");
        name = "转接板";
        break;
    case 4:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]雷达重连成功,升级开始");
        m_anstimer->start(2000);
        break;
    case 5:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]雷达重连失败");
        //ui.pushButton_5->setText("开始升级");
        break;
    case 6:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]擦除原有固件");
        m_anstimer->start(2000);
        break;
    case 7:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[ERRO]发送开始升级指令异常");
        //ui.pushButton_5->setText("开始升级");
        break;
    case 8:
        //ui.progressBar->setValue(0);
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]擦除原有固件完毕，开始发送升级文件");
        break;
    case 9:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[ERRO]擦除原有固件失败");
        //ui.pushButton_5->setText("开始升级");
        break;
    case 10:
        ui.textEdit->setTextColor(Qt::blue);
        ui.textEdit->append("[上位机]固件传输完毕，等待雷达回复");
        m_anstimer->start(2000);
        break;
    case 11:
        ui.textEdit->setTextColor(Qt::blue);
        ui.textEdit->append("[上位机]雷达转接板升级成功，网络自动断开");
        emit disconnectRadar();
        saveUpdatelog(true);
        break;
    case 12:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]远程板开始升级");
        name = "远程板";
        break;
    case 13:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]中程板开始升级");
        name = "中程板";
        break;
    case 14:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]近程板开始升级");
        name = "近程板";
        break;
    case 15:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]ERR1===升级文件保存失败，接收tsm数据过程超时");
        saveUpdatelog(false);
        break;
    case 16:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]ERR2===升级文件保存失败，写FLASH过程存在错误");
        saveUpdatelog(false);
        break;
    case 17:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]ERR3===升级文件保存失败，程序正处于升级过程中，无法处理此命令");
        saveUpdatelog(false);
        break;
    case 18:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]ERR4===升级文件保存失败，应用程序地址不正确");
        saveUpdatelog(false);
        break;
    case 19:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]ERR5===升级文件保存失败，权限不足");
        saveUpdatelog(false);
        break;
    case 20:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]ERR6===升级文件保存失败，用户名/密码错误");
        saveUpdatelog(false);
        break;
    case 21:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]ERR8===升级文件保存失败，格式错误");
        saveUpdatelog(false);
        break;
    case 22:
        ui.textEdit->setTextColor(Qt::darkBlue);
        ui.textEdit->append("[雷达]芯片升级完毕");
        saveUpdatelog(true);
        break;
    case 23:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]EBOOT升级文件保存失败，BOOT版本号出错");
        saveUpdatelog(false);
        break;
    case 24:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]EIAP升级文件保存失败，IAP版本号出错");
        saveUpdatelog(false);
        break;
    case 25:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]EDSP升级文件保存失败，DSP/ARM版本号出错");
        saveUpdatelog(false);
        break;
    case 26:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]EHARD升级文件保存失败，硬件版本号出错");
        saveUpdatelog(false);
        break;
    case 27:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]ELEN升级文件保存失败，接收长度出错");
        saveUpdatelog(false);
        break;
    case 28:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]ENUM升级文件保存失败，接收帧数出错");
        saveUpdatelog(false);
        break;
    case 29:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]ECRC升级文件保存失败，CRC校验异常");
        saveUpdatelog(false);
        break;
    case 30:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]EHED升级文件保存失败，TSM头出错");
        saveUpdatelog(false);
        break;
    case 31:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]E20/0806升级文件保存失败，文件存储地址出错");
        saveUpdatelog(false);
        break;
    case 32:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]GBY1===升级文件保存失败，当前IP已锁定");
        saveUpdatelog(false);
        break;
    case 33:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]GBY2===升级文件保存失败，连接超时");
        saveUpdatelog(false);
        break;
    case 34:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]GBY3===升级文件保存失败，连接端口数量超限，被主动断开");
        saveUpdatelog(false);
        break;
    case 35:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]EMTH===升级文件保存失败，arm操作的芯片和实际芯片不匹配");
        saveUpdatelog(false);
        break;
    case 36:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]CER0===升级文件保存失败，未收到FILC指令");
        saveUpdatelog(false);
        break;
    case 37:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]CER1===升级文件保存失败，FILC文件长度小于等于0");
        saveUpdatelog(false);
        break;
    case 38:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]CER2===升级文件保存失败，Bin文件校验和错误");
        saveUpdatelog(false);
        break;
    case 39:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[雷达]UKNW===升级文件保存失败，升级未知错误");
        saveUpdatelog(false);
        break;
    case 40:
        ui.textEdit->setTextColor(Qt::darkBlue);
        ui.textEdit->append("[雷达]转接板接收升级文件成功，芯片升级开始");
        m_anstimer->start(2000);
        break;
    case 41:
        ui.textEdit->setTextColor(Qt::darkBlue);
        ui.textEdit->append("[雷达]芯片重启成功,擦除芯片原有固件");
        m_anstimer->start(2000);
        break;
    case 42:
        ui.textEdit->setTextColor(Qt::darkBlue);
        ui.textEdit->append("[雷达]芯片flash擦除成功,准备保存tsm文件");
        m_ganstimer->start(3500);
        break;
    case 43:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("设备鉴权完成,查询雷达信息（序列号,BOOT版本,APP版本,硬件特征号,MAC地址）");
        m_anstimer->start(2000);
        break;
    default:
        return;
    }
}

void Update::Slot_Revradarmsg(QString msg,int type){
    //int stopcount = 0;
    m_anstimer->stop();
    switch (type)
    {
    case 0:
        m_serialnum = msg;
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]序列号查询完成");
        m_anstimer->start(1500);
        break;
    case 1:
        m_macaddress = msg;
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]MAC地址查询完成");
        m_anstimer->start(1500);
        break;
    case 2:
        m_bootversion = msg;
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]BOOT版本号查询完成");
        m_anstimer->start(1500);
        break;
    case 3:
        m_appversion = msg;
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]APP版本号查询完成");
        m_anstimer->start(1500);
        break;
    case 4:
        //硬件特征号查询完毕
        m_hardwarenum = msg;
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]硬件特征号查询完成");
        ui.textEdit->setTextColor(Qt::blue);
        ui.textEdit->append("[上位机]全部信息查询完毕");

        if (ui.lineEdit_16->text().size() < 1)
        {
            if (ui.checkBox_3->isChecked())
            {
                updatename[1] = false;
                m_filesize = localFile[1]->size();
                localFile[1]->seek(0);
                tsmfile.clear();
                m_anstimer->start(2000);
                // 读取数据
                tsmfile = localFile[1]->readAll();
                emit sendTsm(tsmfile, m_filesize);
                emit updatecmd(2);
                return;
            }
            if (ui.checkBox_4->isChecked())
            {
                updatename[2] = false;
                m_filesize = localFile[2]->size();
                localFile[2]->seek(0);
                tsmfile.clear();
                m_anstimer->start(2000);
                // 读取数据
                tsmfile = localFile[2]->readAll();
                emit sendTsm(tsmfile, m_filesize);
                emit updatecmd(3);
                return;
            }
            if (ui.checkBox_5->isChecked())
            {
                updatename[3] = false;
                m_filesize = localFile[3]->size();
                localFile[3]->seek(0);
                tsmfile.clear();
                m_anstimer->start(2000);
                // 读取数据
                tsmfile = localFile[3]->readAll();
                emit sendTsm(tsmfile, m_filesize);
                emit updatecmd(4);
                return;
            }
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("[上位机]未勾选任何设备");
            return;
        } else
        {
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("[上位机]重新设置硬件特征号");
            m_setid->start(3000);
            m_setidnum = 0;
            emit sethardid(ui.lineEdit_16->text());
            return;
        }
        break;
    default:
        return;
    }
}

//硬件特征号异常记录
void Update::saveUpdatehardidlog(bool a){
    //ui.pushButton_5->setText("开始升级");
    //保存升级结果
    QString fileName = savepath + "/" + ui.lineEdit_15->text() + "-升级日志.csv";
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly)){
        if (file.open(QFile::ReadWrite))
        {
            QTextStream init(&file);    // 输入流
            //写入标题行
            const char * header = "序号,产品型号,生产批次,备注,批次数量,雷达类型,产品序列号,IP地址,MAC地址,Boot版本,硬件特征号,应用程序版本,转接板,摄像头,远程板,中程板,近程板,作业结果,作业时间,作业人员\n";
            QString headerStr = QString::fromUtf8(header);
            init << headerStr.toUtf8();
            filecount = 2;
            file.close();
        } else
        {
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("[上位机]保存升级记录异常");
            QMessageBox *msgBox1 = new QMessageBox(QMessageBox::Question, "升级", "升级异常，日志保存异常", QMessageBox::Yes);
            msgBox1->setWindowIcon(QIcon(":/folder_sent_mail.ico"));
            // 将原本显示“Yes”的按钮改为显示“是”
            msgBox1->button(QMessageBox::Yes)->setText("重试");
            int res = msgBox1->exec();
            if (QMessageBox::Yes == res){
                //重试
                saveUpdatehardidlog(a);
                return;
            }
        }
    } else{
        QTextStream * in = new QTextStream(&file);//文本流
        QStringList tempOption = in->readAll().split("\n");//每行以\n区分
        filecount = tempOption.count();
        delete in;
        file.close();
    }
    QString current_date = savepath + "/" + ui.lineEdit_15->text() + "-升级日志.csv";
    QFile dataall(current_date);
    if (dataall.open(QFile::ReadWrite | QFile::Append))
    {
        QTextStream out(&dataall);    // 输入流
        //// 时间戳转换为日期时间格式
        if (dataall.size() < 1)
        {
            //写入标题行
            const char * header = "序号,产品型号,生产批次,备注,批次数量,雷达类型,产品序列号,IP地址,MAC地址,Boot版本,硬件特征号,应用程序版本,转接板,远程板,中程板,近程板,摄像头,作业结果,作业时间,作业人员\n";
            QString headerStr = QString::fromUtf8(header);
            out << headerStr.toUtf8();
        }
        QString m_result = "";
        if (a)
        {
            m_result = "不合格，硬件特征号不匹配";
        } else
        {
            m_result = "不合格，硬件特征号设置异常";
        }
        QString m_h7result = "未升级";
        QString m_lrresult = "未升级";
        QString m_mrresult = "未升级";
        QString m_srresult = "未升级";
        QString m_crresult = "未升级";
        QString m_radartype = "";
        if (ui.radioButton_3->isChecked())
        {
            m_radartype = "远程-中程-近程";
        }
        else if (ui.radioButton_4->isChecked())
        {
            m_radartype = "远程-近程";
        }
        else{
            m_radartype = "中程-近程";
        }
        QDateTime  DateTime = QDateTime::currentDateTime();
        QString time = DateTime.toString("yyyy.MM.dd hh:mm:ss");//.zzz
        out << QString(QString::number(filecount - 1)) + "," << ui.lineEdit->text() + "," << ui.lineEdit_3->text() + "," << ui.lineEdit_5->text() + "," << ui.lineEdit_2->text() + "," << m_radartype + "," << m_serialnum + "," << ui.lineEdit_6->text() + "," << m_macaddress + "," << m_bootversion + "," << m_hardwarenum + "," << m_appversion
               + "," << m_h7result + "," << m_lrresult + "," << m_mrresult + "," << m_srresult + "," << m_crresult + "," << m_result + "," << time + "," << ui.lineEdit_4->text() + "," + "\n";
        ui.textEdit->setTextColor(Qt::blue);
        ui.textEdit->append("保存记录完成");
        ui.textEdit->setTextColor(Qt::blue);
        ui.textEdit->append("[上位机]升级流程结束");
        ui.progressBar->setValue(100);
        QMessageBox *msgBox1 = new QMessageBox(QMessageBox::NoIcon, "升级", "升级完毕" + m_result, QMessageBox::Ok, 0);
        msgBox1->button(QMessageBox::Ok)->setText("确定");
        msgBox1->exec();
        delete msgBox1;
        ui.progressBar->setValue(0);
        ui.textEdit->clear();
    } else
    {
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("保存记录异常");
        QMessageBox *msgBox1 = new QMessageBox(QMessageBox::Question, "升级", "升级异常，日志保存异常", QMessageBox::Yes);
        msgBox1->setWindowIcon(QIcon(":/folder_sent_mail.ico"));
        // 将原本显示“Yes”的按钮改为显示“是”
        msgBox1->button(QMessageBox::Yes)->setText("重试");
        int res = msgBox1->exec();
        if (QMessageBox::Yes == res){
            //重试
            saveUpdatehardidlog(a);
            return;
        }
    }

    return;
}

//保存升级记录
void Update::saveUpdatelog(bool result){
    updatednum++;
    float rent = (float)updatednum / (float)updatenum;
    ui.progressBar->setValue(rent * 100);
    if (name == "转接板")
    {
        h7r = result;
    }
    if (name == "远程板")
    {
        lr = result;
    }
    if (name == "中程板")
    {
        mr = result;
    }
    if (name == "近程板")
    {
        sr = result;
    }
    if (name == "摄像头")
    {
        cr = result;
    }
    if (ui.checkBox_2->isChecked() && updatename[0])
    {
        updatename[0] = false;
        m_type = true;
        adbWorker->moveToThread(adbThread);
        disconnect(adbThread, &QThread::started, adbWorker, &ADBWorker::checkdevice);
        disconnect(adbWorker, &ADBWorker::sendADBmsg, this, &Update::Slot_showADBans);
        connect(adbThread, &QThread::started, adbWorker, &ADBWorker::checkdevice);
        connect(adbWorker, &ADBWorker::sendADBmsg, this, &Update::Slot_showADBans,Qt::QueuedConnection);
        adbThread->start();
        return;
    }
    if (ui.checkBox_3->isChecked() && updatename[1])
    {
        if (name == "转接板" || name == "摄像头")
        {
            emit connectRadar(ui.lineEdit_6->text(), ui.lineEdit_7->text().toUShort(), ui.lineEdit_8->text(), ui.lineEdit_9->text(), 0, ui.radioButton->isChecked());
            return;
        }
        updatename[1] = false;
        m_filesize = localFile[1]->size();
        localFile[1]->seek(0);
        tsmfile.clear();
        m_anstimer->start(2000);
        // 读取数据
        tsmfile = localFile[1]->readAll();
        emit sendTsm(tsmfile, m_filesize);
        emit updatecmd(2);
        return;
    }
    if (ui.checkBox_4->isChecked() && updatename[2])
    {
        if (name == "转接板" || name == "摄像头")
        {
            emit connectRadar(ui.lineEdit_6->text(), ui.lineEdit_7->text().toUShort(), ui.lineEdit_8->text(), ui.lineEdit_9->text(), 0, ui.radioButton->isChecked());
            return;
        }
        updatename[2] = false;
        m_filesize = localFile[2]->size();
        localFile[2]->seek(0);
        tsmfile.clear();
        m_anstimer->start(2000);
        // 读取数据
        tsmfile = localFile[2]->readAll();
        emit sendTsm(tsmfile, m_filesize);
        emit updatecmd(3);
        return;
    }
    if (ui.checkBox_5->isChecked() && updatename[3])
    {
        if (name == "转接板" || name == "摄像头")
        {
            emit connectRadar(ui.lineEdit_6->text(), ui.lineEdit_7->text().toUShort(), ui.lineEdit_8->text(), ui.lineEdit_9->text(), 0, ui.radioButton->isChecked());
            return;
        }
        updatename[3] = false;
        m_filesize = localFile[3]->size();
        localFile[3]->seek(0);
        tsmfile.clear();
        m_anstimer->start(2000);
        // 读取数据
        tsmfile = localFile[3]->readAll();
        emit sendTsm(tsmfile, m_filesize);
        emit updatecmd(4);
        return;
    }

    allresult = true;
    //保存升级结果
    QString fileName = savepath + "/" + ui.lineEdit_15->text() + "-升级日志.csv";
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly)){
        if (file.open(QFile::ReadWrite))
        {
            QTextStream init(&file);    // 输入流
            //写入标题行
            const char * header = "序号,产品型号,生产批次,备注,批次数量,雷达类型,产品序列号,IP地址,MAC地址,Boot版本,硬件特征号,应用程序版本,转接板,远程板,中程板,近程板,摄像头,作业结果,作业时间,作业人员\n";
            QString headerStr = QString::fromUtf8(header);
            init << headerStr.toUtf8();
            filecount = 2;
            file.close();
        } else
        {
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("[上位机]保存升级记录异常");
            QMessageBox *msgBox1 = new QMessageBox(QMessageBox::Question, "升级", "升级异常，日志保存异常", QMessageBox::Yes);
            msgBox1->setWindowIcon(QIcon(":/folder_sent_mail.ico"));
            // 将原本显示“Yes”的按钮改为显示“是”
            msgBox1->button(QMessageBox::Yes)->setText("重试");
            int res = msgBox1->exec();
            if (QMessageBox::Yes == res){
                //重试
                saveUpdatelog(result);
                return;
            }
        }
    } else{
        QTextStream * in = new QTextStream(&file);//文本流
        QStringList tempOption = in->readAll().split("\n");//每行以\n区分
        filecount = tempOption.count();
        delete in;
        file.close();
    }
    QString current_date = savepath + "/" + ui.lineEdit_15->text() + "-升级日志.csv";
    QFile dataall(current_date);
    if (dataall.open(QFile::ReadWrite | QFile::Append))
    {
        QTextStream out(&dataall);    // 输入流
        //// 时间戳转换为日期时间格式
        if (dataall.size() < 1)
        {
            //写入标题行
            const char * header = "序号,产品型号,生产批次,备注,批次数量,雷达类型,产品序列号,IP地址,MAC地址,Boot版本,硬件特征号,应用程序版本,转接板,远程板,中程板,近程板,摄像头,作业结果,作业时间,作业人员\n";
            QString headerStr = QString::fromUtf8(header);
            out << headerStr.toUtf8();
        }
        QString m_result = "";
        QString m_h7result = "未升级";
        QString m_lrresult = "未升级";
        QString m_mrresult = "未升级";
        QString m_srresult = "未升级";
        QString m_crresult = "未升级";
        QString m_radarname = "";
        if (ui.checkBox_6->isChecked())
        {
            if (cr)
            {
                QFileInfo fileInfo(localFile[0]->fileName());
                QString filename(fileInfo.fileName());
                int index = filename.indexOf("tsm");
                filename.remove(index - 1, 4);
                m_cameraversion = "";
                m_cameraversion = filename;
                m_crresult = filename;
            }
            else
            {
                allresult = false;
                m_crresult = "不合格";
            }

        }
        if (ui.checkBox_2->isChecked())
        {
            if (h7r)
            {
                QFileInfo fileInfo(localFile[4]->fileName());
                QString filename(fileInfo.fileName());
                int index = filename.indexOf("tsm");
                filename.remove(index - 1, 4);
                m_appversion = "";
                m_appversion = filename;
                m_h7result = filename;
            }
            else
            {
                allresult = false;
                m_h7result = "不合格";
            }

        }
        if (ui.checkBox_3->isChecked())
        {
            if (lr)
            {
                QFileInfo fileInfo(localFile[1]->fileName());
                QString filename(fileInfo.fileName());
                int index = filename.indexOf("tsm");
                filename.remove(index - 1, 4);
                m_lrresult = filename;
            }
            else
            {
                allresult = false;
                m_lrresult = "不合格";
            }

        }
        if (ui.checkBox_4->isChecked())
        {
            if (mr)
            {
                QFileInfo fileInfo(localFile[2]->fileName());
                QString filename(fileInfo.fileName());
                int index = filename.indexOf("tsm");
                filename.remove(index - 1, 4);
                m_mrresult = filename;
            }
            else
            {
                allresult = false;
                m_mrresult = "不合格";
            }

        }
        if (ui.checkBox_5->isChecked())
        {
            if (sr)
            {
                QFileInfo fileInfo(localFile[3]->fileName());
                QString filename(fileInfo.fileName());
                int index = filename.indexOf("tsm");
                filename.remove(index - 1, 4);
                m_srresult = filename;
            }
            else
            {
                allresult = false;
                m_srresult = "不合格";

            }

        }

        //保存
        if (allresult)
        {
            m_result = "合格";
        } else
        {
            m_result = "不合格";
        }
        if (ui.radioButton_3->isChecked())
        {
            m_radarname = "远程-中程-近程";
        }
        else if (ui.radioButton_4->isChecked())
        {
            m_radarname = "远程-近程";
        }
        else
        {
            m_radarname = "中程-近程";
        }
        QDateTime  DateTime = QDateTime::currentDateTime();
        QString time = DateTime.toString("yyyy.MM.dd hh:mm:ss");//.zzz
        out << QString(QString::number(filecount - 1)) + "," << ui.lineEdit->text() + "," << ui.lineEdit_3->text() + "," << ui.lineEdit_5->text() + "," << ui.lineEdit_2->text() + "," << m_radarname  + ","<< m_serialnum + "," << ui.lineEdit_6->text() + "," << m_macaddress + "," << m_bootversion + "," << m_hardwarenum + "," << m_appversion
               + "," << m_h7result + "," << m_lrresult + "," << m_mrresult + "," << m_srresult + "," << m_crresult + "," << m_result + "," << time + "," << ui.lineEdit_4->text() + "," + "\n";
        ui.textEdit->setTextColor(Qt::blue);
        ui.textEdit->append("保存记录完成");
        ui.textEdit->setTextColor(Qt::blue);
        ui.textEdit->append("[上位机]升级流程结束");
        ui.progressBar->setValue(100);
        QMessageBox *msgBox1 = new QMessageBox(QMessageBox::NoIcon, "升级", "升级完毕" + m_result, QMessageBox::Ok, 0);
        msgBox1->button(QMessageBox::Ok)->setText("确定");
        msgBox1->exec();
        delete msgBox1;
        ui.progressBar->setValue(0);
        ui.textEdit->clear();
    } else
    {
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("保存记录异常");
        QMessageBox *msgBox1 = new QMessageBox(QMessageBox::Question, "升级", "升级异常，日志保存异常", QMessageBox::Yes);
        msgBox1->setWindowIcon(QIcon(":/folder_sent_mail.ico"));
        // 将原本显示“Yes”的按钮改为显示“是”
        msgBox1->button(QMessageBox::Yes)->setText("重试");
        int res = msgBox1->exec();
        if (QMessageBox::Yes == res){
            //重试
            saveUpdatelog(result);
            return;
        }
    }

    return;
}

void Update::Slot_Showtimeout(int cmdtype){
    m_anstimer->stop();
    switch (cmdtype)
    {
    case THREADCMD_TYPE_AUTH:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次进行鉴权登录");
        m_anstimer->start(1500);
        break;
    case THREADCMD_TYPE_SERIAL:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次查询硬件特征号");
        m_anstimer->start(1500);
        break;
        //mac地址
    case THREADCMD_TYPE_SERNET:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次查询Mac地址");
        m_anstimer->start(1500);
        break;
        //boot版本号
    case THREADCMD_TYPE_BOOT:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次查询BOOT版本号");
        m_anstimer->start(1500);
        break;
        //app版本号
    case THREADCMD_TYPE_APP:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次查询APP版本号");
        m_anstimer->start(1500);
        break;
        //硬件特征号
    case THREADCMD_TYPE_HARDWARE:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次查询硬件特征号");
        break;
    case THREADCMD_TYPE_H7VERSION:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次比对版本信息");
        m_anstimer->start(1500);
        //m_choosefile[0] = false;
        break;
    case THREADCMD_TYPE_RESTART:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次重启雷达");
        m_anstimer->start(1500);
        break;
    case THREADCMD_TYPE_H7INIT:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次下发开始升级指令");
        m_anstimer->start(1500);
        break;
    case THREADCMD_TYPE_H7ERAS:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次下发擦除原有固件指令");
        m_anstimer->start(1500);
        break;
    case THREADCMD_TYPE_H7TSM:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]升级文件下发后未收到雷达回复");
        //保存升级记录
        saveUpdatelog(false);
        break;
    case THREADCMD_TYPE_FSTART:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次下发远程板开始升级指令");
        m_anstimer->start(1500);
        break;
    case THREADCMD_TYPE_MSTART:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次下发中程板开始升级指令");
        m_anstimer->start(1500);
        break;
    case THREADCMD_TYPE_NSTART:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次下发近程板开始升级指令");
        m_anstimer->start(1500);
        break;
    case THREADCMD_TYPE_FTSM:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]升级文件下发后未收到雷达回复");
        //保存升级记录
        saveUpdatelog(false);
        break;
    default:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]上述功能指令接收回复失败");
        return;
    }

}

void Update::Slot_EnvironmentChange(int id,bool check){

    if ((id == 0 && check == true) || (id == 1 && check == false)){
        //ui.lineEdit_8->setVisible(true);
        ui.groupBox_9->setVisible(true);
        //ui.lineEdit_9->setVisible(true);
        //ui.label_8->setVisible(true);
        //ui.label_10->setVisible(true);
        ui.groupBox_4->setVisible(false);
        //ui.pushButton_10->setVisible(true);
    }
    else if ((id == 0 && check == false) || (id == 1 && check == true)){
        ui.lineEdit_8->setVisible(false);
        ui.lineEdit_9->setVisible(false);
        ui.label_8->setVisible(false);
        ui.label_10->setVisible(false);
        ui.groupBox_6->setVisible(true);
        ui.groupBox_4->setVisible(true);
        ui.pushButton_10->setVisible(false);
        ui.groupBox_9->setVisible(false);
    }
}

//关闭软件
void Update::closeEvent(QCloseEvent *event) {
    QMessageBox *msgBox = new QMessageBox(QMessageBox::Question, tr("程序关闭"), "<font color='black'>即将关闭程序，是否继续？", QMessageBox::Yes | QMessageBox::No);
    msgBox->button(QMessageBox::Yes)->setText("是");
    msgBox->button(QMessageBox::No)->setText("否");
    int res = msgBox->exec();
    if (QMessageBox::Yes == res){
        event->accept();
    } else
    {
        event->ignore();  //忽略退出信号，程序继续运行
    }
}

void Update::Slot_sethardidover(){
    m_setid->stop();
    ui.textEdit->setTextColor(Qt::blue);
    ui.textEdit->append("[上位机]硬件特征号设置完成");
    m_hardwarenum = ui.lineEdit_16->text().mid(0,12);
    //非鉴权升级关闭BOOT
    //emit closebootkey();
    //ui.textEdit->setTextColor(Qt::red);
    //ui.textEdit->append("[上位机]BOOT鉴权功能关闭");
    if (ui.checkBox_3->isChecked())
    {
        updatename[1] = false;
        m_filesize = localFile[1]->size();
        localFile[1]->seek(0);
        tsmfile.clear();
        m_anstimer->start(2000);
        // 读取数据
        tsmfile = localFile[1]->readAll();
        emit sendTsm(tsmfile, m_filesize);
        emit updatecmd(2);
        return;
    }
    if (ui.checkBox_4->isChecked())
    {
        updatename[2] = false;
        m_filesize = localFile[2]->size();
        localFile[2]->seek(0);
        tsmfile.clear();
        m_anstimer->start(2000);
        // 读取数据
        tsmfile = localFile[2]->readAll();
        emit sendTsm(tsmfile, m_filesize);
        emit updatecmd(3);
        return;
    }
    if (ui.checkBox_5->isChecked())
    {
        updatename[3] = false;
        m_filesize = localFile[3]->size();
        localFile[3]->seek(0);
        tsmfile.clear();
        m_anstimer->start(2000);
        // 读取数据
        tsmfile = localFile[3]->readAll();
        emit sendTsm(tsmfile, m_filesize);
        emit updatecmd(4);
        return;
    }

    ui.textEdit->setTextColor(Qt::red);
    ui.textEdit->append("[上位机]未勾选任何设备");
    return;
}

void Update::Slot_savefiledir(){
    if (ui.lineEdit_15->text().size() < 1)
    {
        QMessageBox *msgBox1 = new QMessageBox(QMessageBox::NoIcon, "日志", "日志名字不能为空", QMessageBox::Ok, 0);
        msgBox1->setWindowIcon(QIcon(":/folder_sent_mail.ico"));
        // 将原本显示“Yes”的按钮改为显示“是”
        msgBox1->button(QMessageBox::Ok)->setText("确定");
        msgBox1->exec();
        return;
    }
    //LastSaveFile
    QSettings setting("FileAddress.ini", QSettings::IniFormat);
    setting.setIniCodec(QTextCodec::codecForName("GB2312"));
    QString lastpath;
    lastpath = setting.value("File/LastSaveFile").toString();
    savepath = QFileDialog::getExistingDirectory(this, tr("Open Directory"), lastpath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    setting.setValue("File/LastSaveFile", savepath);
    if (savepath == "")
    {
        m_setsavepath = false;
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]日志保存路径配置失败");
    } else
    {
        m_setsavepath = true;
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]日志保存路径配置完成");
        ui.textEdit->append(savepath + ui.lineEdit_15->text() + "-升级日志.csv");
    }
    return;
}

void Update::Slot_h7tsmfile(){

}

void Update::Slot_lrrtsmfile(){
    if (!m_setsavepath)
    {
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]未配置日志保存路径");
        return;
    }
    m_choosefile[1] = false;
    QSettings setting("FileAddress.ini", QSettings::IniFormat);
    setting.setIniCodec(QTextCodec::codecForName("GB2312"));
    QString lastpath;
    lastpath = setting.value("File/LastFile").toString();
    filepath[1] = QFileDialog::getOpenFileName(this, "open file", lastpath, "text file(*.tsm);;All file(*.*)");
    if (!filepath[1].isEmpty()){
        setting.setValue("File/LastFile", filepath[1]);
    }

    localFile[1] = new QFile(filepath[1]);

    if (!localFile[1]->open((QFile::ReadOnly)))
    {
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]打开升级文件失败");
        return;
    } else
    {
        QByteArray msg;
        msg = localFile[1]->read(48);
        msg.remove(0, 16);
        if (msg.at(1) != 48){
            m_choosefile[1] = false;
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("[上位机]非远程板升级文件");
            return;
        }
        ui.textEdit->setTextColor(Qt::darkYellow);
        ui.textEdit->append("[上位机]远程板已选择升级文件：" + filepath[1]);
        //固件选择标志位置真值
        m_choosefile[1] = true;
        QFileInfo fileInfo(localFile[1]->fileName());
        ui.lineEdit_12->setStyleSheet("color:rgb(255,0,0)");
        ui.lineEdit_12->setText(fileInfo.fileName());
    }

}
void Update::Slot_mrrtsmfile(){
    if (!m_setsavepath)
    {
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]未配置日志保存路径");
        return;
    }
    m_choosefile[2] = false;
    QSettings setting("FileAddress.ini", QSettings::IniFormat);
    setting.setIniCodec(QTextCodec::codecForName("GB2312"));
    QString lastpath;
    lastpath = setting.value("File/LastFile").toString();
    filepath[2] = QFileDialog::getOpenFileName(this, "open file", lastpath, "text file(*.tsm);;All file(*.*)");
    if (!filepath[2].isEmpty()){
        setting.setValue("File/LastFile", filepath[2]);
    }

    localFile[2] = new QFile(filepath[2]);

    if (!localFile[2]->open((QFile::ReadOnly)))
    {
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]打开升级文件失败");
        return;
    } else
    {
        QByteArray msg;
        msg = localFile[2]->read(48);
        msg.remove(0, 16);
        if (msg.at(1) != 49){
            m_choosefile[2] = false;
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("[上位机]非中程板升级文件");
            return;
        }
        ui.textEdit->setTextColor(Qt::blue);
        ui.textEdit->append("[上位机]中程板已选择升级文件：" + filepath[2]);
        QFileInfo fileInfo(localFile[2]->fileName());
        ui.lineEdit_13->setStyleSheet("color:rgb(255,0,0)");
        ui.lineEdit_13->setText(fileInfo.fileName());
        //固件选择标志位置真值
        m_choosefile[2] = true;
    }

}
void Update::Slot_srrtsmfile(){
    if (!m_setsavepath)
    {
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]未配置日志保存路径");
        return;
    }
    m_choosefile[3] = false;
    QSettings setting("FileAddress.ini", QSettings::IniFormat);
    setting.setIniCodec(QTextCodec::codecForName("GB2312"));
    QString lastpath;
    lastpath = setting.value("File/LastFile").toString();
    filepath[3] = QFileDialog::getOpenFileName(this, "open file", lastpath, "text file(*.tsm);;All file(*.*)");
    if (!filepath[3].isEmpty()){
        setting.setValue("File/LastFile", filepath[3]);
    }

    localFile[3] = new QFile(filepath[3]);

    if (!localFile[3]->open((QFile::ReadOnly)))
    {
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]打开升级文件失败");
        return;
    } else
    {
        QByteArray msg;
        msg = localFile[3]->read(48);
        msg.remove(0, 16);
        if (msg.at(1) != 50){
            m_choosefile[3] = false;
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("[上位机]非近程板升级文件");
            return;
        }
        ui.textEdit->setTextColor(Qt::darkGreen);
        ui.textEdit->append("[上位机]近程板已选择升级文件：" + filepath[3]);
        QFileInfo fileInfo(localFile[3]->fileName());
        ui.lineEdit_14->setStyleSheet("color:rgb(255,0,0)");
        ui.lineEdit_14->setText(fileInfo.fileName());
        //固件选择标志位置真值
        m_choosefile[3] = true;
    }

}

void Update::Slot_setidout(){
    m_setid->stop();
    if (m_setidnum < 2)
    {
        m_setidnum++;
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]再次设置硬件特征号");
        m_setid->start(3000);
        emit sethardid(ui.lineEdit_16->text());
        return;
    }
    else
    {
        m_setidnum = 0;
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]设置硬件特征号失败");
        saveUpdatehardidlog(false);
        return;
    }
}

void Update::choose50(){
    ui.checkBox_3->setVisible(true);
    ui.pushButton_11->setVisible(true);
    ui.checkBox_4->setVisible(true);
    ui.pushButton_12->setVisible(true);
    ui.checkBox_5->setVisible(true);
    ui.pushButton_13->setVisible(true);
    ui.label_13->setVisible(true);
    ui.lineEdit_12->setVisible(true);
    ui.label_14->setVisible(true);
    ui.lineEdit_13->setVisible(true);
    ui.label_15->setVisible(true);
    ui.lineEdit_14->setVisible(true);
}

void Update::choose35(){
    ui.checkBox_3->setVisible(true);
    ui.pushButton_11->setVisible(true);
    ui.checkBox_4->setVisible(false);
    ui.checkBox_4->setChecked(false);
    ui.pushButton_12->setVisible(false);
    ui.checkBox_5->setVisible(true);
    ui.pushButton_13->setVisible(true);
    ui.label_13->setVisible(true);
    ui.lineEdit_12->setVisible(true);
    ui.label_14->setVisible(false);
    ui.lineEdit_13->setVisible(false);
    ui.lineEdit_13->clear();
    ui.label_15->setVisible(true);
    ui.lineEdit_14->setVisible(true);
}

void Update::choose30(){
    ui.checkBox_3->setVisible(false);
    ui.pushButton_11->setVisible(false);
    ui.checkBox_3->setChecked(false);
    ui.checkBox_4->setVisible(true);
    ui.pushButton_12->setVisible(true);
    ui.checkBox_5->setVisible(true);
    ui.pushButton_13->setVisible(true);
    ui.label_13->setVisible(false);
    ui.lineEdit_12->setVisible(false);
    ui.lineEdit_12->clear();
    ui.label_14->setVisible(true);
    ui.lineEdit_13->setVisible(true);
    ui.label_15->setVisible(true);
    ui.lineEdit_14->setVisible(true);
}


//发送SSH指令
void Update::on_pushButton_14_clicked()
{
    QString cmd = ui.lineEdit_17->text();
    if(cmd.size() < 1){
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]指令为空");
        return;
    }
    emit sendSSHcmd(cmd);
    //    cmd += "\n";
    //    shell->write(cmd.toLatin1().data());
}


void Update::on_pushButton_15_clicked()
{
    m_choosefile[4] = false;
    QSettings setting("FileAddress.ini", QSettings::IniFormat);
    setting.setIniCodec(QTextCodec::codecForName("GB2312"));
    QString lastpath;
    lastpath = setting.value("File/LastFile").toString();
    filepath[4] = QFileDialog::getOpenFileName(this, "open file", lastpath, "text file(*.tsm);;All file(*.*)");
    if (!filepath[4].isEmpty()){
        setting.setValue("File/LastFile", filepath[4]);
    }

    localFile[4] = new QFile(filepath[4]);

    if (!localFile[4]->open((QFile::ReadOnly)))
    {
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]打开升级文件失败");
        return;
    }else{
        QByteArray msg;
        msg = localFile[4]->read(48);
        msg.remove(0, 16);

        if (ui.lineEdit_16->text().size() > 1)
        {
            msg.remove(0, 10);
            QString strData;
            strData = QString::fromLocal8Bit(msg.mid(0, 6).toHex().toUpper());
            if (strData != ui.lineEdit_16->text()){
                ui.textEdit->setTextColor(Qt::red);
                ui.textEdit->append("[上位机]转接板升级文件不匹配");
                return;
            }
        }
        m_choosefile[4] = true;
        QFileInfo fileInfo(localFile[4]->fileName());
        ui.lineEdit_11->setStyleSheet("color:rgb(255,0,0)");
        ui.lineEdit_11->setText(fileInfo.fileName());
        ui.textEdit->setTextColor(Qt::darkGreen);
        ui.textEdit->append("[上位机]已选择转接板升级文件：" + filepath[4]);
        localFile[4]->close();
        return;
    }

}

//连接
void Update::on_pushButton_clicked()
{
    if (ui.lineEdit_6->text().size() < 1 || ui.lineEdit_7->text().size() < 1)
    {
        ui.textEdit->clear();
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("IP地址或端口号未填写");
        return;
    }
    if (ui.radioButton->isChecked() && ui.lineEdit_9->text().size() < 1)
    {
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("未填写密码");
        return;
    }
    if (ui.radioButton->isChecked() && ui.lineEdit_8->text().size() < 1)
    {
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("未填写用户名");
        return;
    }


    if(ui.radioButton->isChecked()){
        if(ui.pushButton->text() == "连接"){
            //m_sshcon->SSHconnect(ui.lineEdit_6->text(),ui.lineEdit_7->text().toInt(),ui.lineEdit_8->text(),ui.lineEdit_9->text());
        }else{
            ui.pushButton->setText("连接");
            //m_sshcon->SSHdisconnect();
        }
    }else{
        if(ui.pushButton->text() == "连接"){
            ui.textEdit->clear();
            ui.textEdit->setTextColor(Qt::black);
            ui.textEdit->append("连接设备，等待设备进入工作状态");
            m_serialnum = "";
            m_macaddress = "";
            m_bootversion = "CSR_TN_BOOT";
            m_appversion = "";
            m_hardwarenum = "";
            emit connectRadar(ui.lineEdit_6->text(), ui.lineEdit_7->text().toUShort(), ui.lineEdit_8->text(), ui.lineEdit_9->text(), 0, ui.radioButton->isChecked());
        }else{
            ui.pushButton->setText("连接");
            m_ganstimer->stop();
            m_anstimer->stop();
            emit disconnectRadar();
        }
    }

}

//预定义显示
void Update::Slot_showsshmsg(int res){
    QString dir = ui.lineEdit_18->text();
    switch(res){
    case 1:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("SSH连接成功");
        ui.pushButton->setText("断开");
        //m_sshcon->upload(filepath[4],dir);
        //delete localFile[4];
        break;
    case 2:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("SSH断开成功");
        ui.pushButton->setText("连接");
        break;
    case 3:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("SSH开始传输文件");
        break;
    case 4:
        break;
    case 5:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("SSH指令下发完毕");
        break;
    case 6:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("SSH未建立连接");
        break;
    case 7:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("SFTP通道未建立");
        break;
    }
}

//SSH消息显示
void Update::Slot_showsshrevmsg(QString sshmsg){
    ui.textEdit->setTextColor(Qt::black);
    if(sshmsg.contains("sshConnectError")){
        ui.textEdit->setTextColor(Qt::red);
    }
    ui.textEdit->append(sshmsg);
}

//连接成功
void Update::Slot_connected(){
    disconnect(m_socket, SIGNAL(readyRead()), this, SLOT(Slot_revmsg()));
    connect(m_socket, SIGNAL(readyRead()), this, SLOT(Slot_revmsg()));
    m_recontimer->stop();
    m_recontimer->start(5000);
    m_workflag = 1;
    m_socket->write("INIT");
    ui.textEdit->setTextColor(Qt::red);
    ui.textEdit->append("发送校验指令");
}

//断开成功
void Update::Slot_disconnected(){
//    ui.textEdit->setTextColor(Qt::red);
//    ui.textEdit->append("雷达已断开");
}

//接收消息
void Update::Slot_revmsg(){
    QByteArray m_recary;
    QByteArray data;
    QByteArray hardid;
    int index = -1;
    m_recary = m_socket->readAll();
    switch(m_workflag){
    case 1:
        if(m_recary.contains("DONE")){
            m_workflag = -1;
            m_recontimer->stop();
            ui.textEdit->setTextColor(Qt::blue);
            if(m_type){
                ui.textEdit->append("转接板版本校验无异常");
            }else{
                ui.textEdit->append("摄像头版本校验无异常");
                if(ui.checkBox_2->isChecked()){
                    name = "摄像头";
                    saveUpdatelog(true);
                    return;
                }
            }

            if(ui.lineEdit_16->text().size() < 1){
                m_recontimer->start(5000);
                checkversionnum = 0;
                ui.textEdit->append("查询序列号");
                data.resize(4);
                data[0] = 0x52;
                data[1] = 0x48;
                data[2] = 0x53;
                data[3] = 0x4E;
                m_socket->write(data);
                m_workflag = 3;
                return;
            }else{
                m_recontimer->start(5000);
                checkversionnum = 0;
                m_hardwarenum = ui.lineEdit_16->text().mid(0,12);
                ui.textEdit->append("设置新硬件特征号");
                data[0] = 0x53;
                data[1] = 0x48;
                data[2] = 0x49;
                data[3] = 0x44;
                data[4] = 0x00;
                data[5] = 0x00;
                data[6] = 0x00;
                data[7] = 0x06;
                hardid = hexStrToByteArray(ui.lineEdit_16->text());
                data.append(hardid);
                m_socket->write(data);
                m_workflag = 2;
                return;
            }

        }
        break;
    case 2:
        if(m_recary.contains("PHID")){
            m_workflag = -1;
            ui.textEdit->append("硬件特征号设置完成");
            m_recontimer->start(5000);
            checkversionnum = 0;
            ui.textEdit->append("查询序列号");
            data.resize(4);
            data[0] = 0x52;
            data[1] = 0x48;
            data[2] = 0x53;
            data[3] = 0x4E;
            m_socket->write(data);
            m_workflag = 3;
            return;
        }
        break;
    case 3:
        for (int i = 0; i <= m_recary.size(); i++){
            //处置多帧混合
            if (m_recary.left(4) != "PHSN"){
                m_recary.remove(0, 1);
                continue;
            }else{
                m_recontimer->stop();
                m_recontimer->start(5000);
                checkversionnum = 0;
                m_workflag = -1;
                m_recary.remove(0, 8);
                index = m_recary.indexOf(0xFF);
                QString strData;
                strData = QString::fromLatin1(m_recary.mid(0, index));
                m_serialnum = strData;
                ui.textEdit->append("查询序列号完成" + strData);
                ui.textEdit->append("查询MAC地址");
                data.resize(4);
                data[0] = 0x53;
                data[1] = 0x52;
                data[2] = 0x4E;
                data[3] = 0x54;
                m_socket->write(data);
                m_workflag = 4;
                return;
            }
        }
        break;
    case 4:
        for (int i = 0; i <= m_recary.size(); i++){
            //处置多帧混合
            if (m_recary.left(4) != "PANT"){
                m_recary.remove(0, 1);
                continue;
            } else{
                m_recontimer->stop();
                checkversionnum = 0;
                m_workflag = -1;
                QString mac;
                m_recary.remove(0, 8);
                //MAC
                mac = QString("%1:%2:%3:%4:%5:%6").arg((uchar)m_recary[14], 2, 16, QChar('0')).arg((uchar)m_recary[15], 2, 16, QChar('0')).arg((uchar)m_recary[16], 2, 16, QChar('0')).arg((uchar)m_recary[17], 2, 16, QChar('0')).arg((uchar)m_recary[18], 2, 16, QChar('0')).arg((uchar)m_recary[19], 2, 16, QChar('0'));
                ui.textEdit->append("查询Mac地址完成" + mac);
                //m_socket->abort();
                m_macaddress = mac;
                m_bootversion = "CSR_TN_BOOT";
                ui.textEdit->append("查询APP版本号");
                data.resize(12);
                data[0] = 0x44;
                data[1] = 0x53;
                data[2] = 0x46;
                data[3] = 0x31;
                data[4] = 0x00;
                data[5] = 0x00;
                data[6] = 0x00;
                data[7] = 0x04;
                data[8] = 0x52;
                data[9] = 0x50;
                data[10] = 0x52;
                data[11] = 0x4D;
                m_socket->write(data);
                m_workflag = 5;
                m_recontimer->start(5000);
                checkversionnum = 0;
                return;
            }
        }
        break;
    case 5:
        for (int i = 0; i <= m_recary.size(); i++){
            //处置多帧混合
            if (m_recary.left(4) != "RPRM"){
                m_recary.remove(0, 1);
                continue;
            } else{
                m_recontimer->stop();
                checkversionnum = 0;
                m_workflag = -1;
                m_recary.remove(0, 8);
                QString strData;
                strData = QString::fromLocal8Bit(m_recary);
                m_appversion.clear();
                m_appversion.append(strData);
                m_socket->abort();
                ui.textEdit->append("查询APP版本号完成" + m_appversion);
                if(m_type){
                    name = "转接板";
                    saveUpdatelog(true);
                    return;
                }else{
                    name = "摄像头";
                    saveUpdatelog(true);
                    return;
                }


            }
        }
        break;
    }


}

void Update::Slot_reconnect(){
    m_recontimer->stop();
    QByteArray data;
    QString hardid = "";
    if(checkversionnum < 2){
        checkversionnum++;
        m_recontimer->start(5000);
        switch(m_workflag){
        case 0:
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("升级超时");
            m_socket->abort();
            name = "转接板";
            saveUpdatelog(false);
            checkversionnum = 0;
            return;
        case 1:
            m_workflag = 1;
            m_socket->write("INIT");
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("重新发送校验指令");
            return;
        case 2:
            m_hardwarenum = ui.lineEdit_16->text().mid(0,12);
            ui.textEdit->append("重新发送设置新硬件特征号");
            data[0] = 0x53;
            data[1] = 0x48;
            data[2] = 0x49;
            data[3] = 0x44;
            data[4] = 0x00;
            data[5] = 0x00;
            data[6] = 0x00;
            data[7] = 0x06;
            hardid = hexStrToByteArray(ui.lineEdit_16->text());
            data.append(hardid);
            m_socket->write(data);
            m_workflag = 2;
            return;
        case 3:
            ui.textEdit->append("重新发送查询序列号指令");
            data.resize(4);
            data[0] = 0x52;
            data[1] = 0x48;
            data[2] = 0x53;
            data[3] = 0x4E;
            m_socket->write(data);
            m_workflag = 3;
            return;
        case 4:
            ui.textEdit->append("重新发送查询Mac地址指令");
            data.resize(4);
            data[0] = 0x53;
            data[1] = 0x52;
            data[2] = 0x4E;
            data[3] = 0x54;
            m_socket->write(data);
            m_workflag = 4;
            return;
        case 5:
            ui.textEdit->append("重新发送查询APP版本号指令");
            data.resize(12);
            data[0] = 0x44;
            data[1] = 0x53;
            data[2] = 0x46;
            data[3] = 0x31;
            data[4] = 0x00;
            data[5] = 0x00;
            data[6] = 0x00;
            data[7] = 0x04;
            data[8] = 0x52;
            data[9] = 0x50;
            data[10] = 0x52;
            data[11] = 0x4D;
            m_socket->write(data);
            m_workflag = 5;
            return;
        }
    }else{
        if(m_type){
            name = "转接板";
        }else{
            name = "摄像头";
        }
        switch(m_workflag){
        case 0:
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("升级超时");
            m_socket->abort();
            //name = "转接板";
            saveUpdatelog(false);
            checkversionnum = 0;
            return;
        case 1:
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("升级异常，版本号无法校验");
            m_socket->abort();
            //name = "转接板";
            saveUpdatelog(false);
            checkversionnum = 0;
            return;
        case 2:
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("设置新硬件特征号异常");
            m_socket->abort();
            //name = "转接板";
            saveUpdatelog(false);
            checkversionnum = 0;
            return;
        case 3:
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("查询序列号异常");
            m_socket->abort();
            //name = "转接板";
            saveUpdatelog(false);
            checkversionnum = 0;
            return;
        case 4:
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("查询Mac地址异常");
            m_socket->abort();
            //name = "转接板";
            saveUpdatelog(false);
            checkversionnum = 0;
            return;
        case 5:
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("查询APP版本号异常");
            m_socket->abort();
            //name = "转接板";
            saveUpdatelog(false);
            checkversionnum = 0;
            return;
        }
    }

}




void Update::Slot_checkversion(){
    m_readb->stop();
    QProcess adbProcess;
    // 执行ADB连接命令
    QStringList adbConnectArgs;
    adbConnectArgs << "devices";
    adbProcess.start("adb", adbConnectArgs);
    adbProcess.waitForFinished();


    // 构建ADB连接命令
    adbConnectArgs.clear();
    adbConnectArgs << "disconnect";
    // 创建一个QProcess对象来执行ADB命令
    // 执行ADB连接命令
    adbProcess.start("adb", adbConnectArgs);
    adbProcess.waitForFinished();
    QString output = adbProcess.readAllStandardOutput();
    QString errorOutput = adbProcess.readAllStandardError();
    // 检查执行结果
    qDebug() << output;
    qDebug() << errorOutput;
    adbConnectArgs.clear();
    if(output.contains("disconnected")){
        adbConnectArgs << "connect" << m_radarip;
        // 创建一个QProcess对象来执行ADB命令
        QProcess adbProcess;
        // 执行ADB连接命令
        adbProcess.start("adb", adbConnectArgs);
        adbProcess.waitForFinished();
    }
    if(ui.checkBox_2->isChecked() && m_type){
        QString adbShellCommand = "adb shell find /root -name radarTSMTC*";
        // 创建一个QProcess对象来执行ADB命令
        QProcess adbProcess;

        // 执行ADB shell命令
        adbProcess.start(adbShellCommand);
        adbProcess.waitForFinished();
        QString output = adbProcess.readAllStandardOutput();
        QString errorOutput = adbProcess.readAllStandardError();
        qDebug() << output;
        qDebug() << errorOutput;
        QString tsmname = ui.lineEdit_11->text();
        int index = tsmname.indexOf(".tsm");
        if(index > 0){
            tsmname.remove(index,4);
        }
        // 检查执行结果
        if (adbProcess.exitCode() == 0) {
            if(output.contains(tsmname)){
                m_recontimer->start(5000);
                m_workflag = 0;
                m_socket->abort();
                m_socket->connectToHost(ui.lineEdit_6->text(),5000);
                return;
            }else{
                //ui.pushButton_5->setText("开始升级");
                ui.textEdit->setTextColor(Qt::red);
                ui.textEdit->append("升级失败，雷达版本号不匹配");
                return;
            }
        } else {
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("升级失败，雷达版本号校验异常");
            return;
        }

    }
    if(ui.checkBox_6->isChecked() && (!m_type)){
        QString adbShellCommand = "adb shell find /root -name atk_yolov5*";

        // 创建一个QProcess对象来执行ADB命令
        QProcess adbProcess;

        // 执行ADB shell命令
        adbProcess.start(adbShellCommand);
        adbProcess.waitForFinished();
        QString output = adbProcess.readAllStandardOutput();
        QString errorOutput = adbProcess.readAllStandardError();
        qDebug() << output;
        qDebug() << errorOutput;
        QString tsmname = ui.lineEdit_19->text();
        int index = tsmname.indexOf(".tsm");
        if(index > 0){
            tsmname.remove(index,4);
        }
        // 检查执行结果
        if (adbProcess.exitCode() == 0) {
            if(output.contains(tsmname)){
                m_recontimer->start(5000);
                m_workflag = 0;
                m_socket->abort();
                m_socket->connectToHost(ui.lineEdit_6->text(),5000);
                return;
//                checkversionnum = 0;
//                m_checkversiontimer->start(8000);
            }else{
                ui.textEdit->setTextColor(Qt::red);
                ui.textEdit->append("升级失败，摄像头版本号不匹配");
                return;
            }
        } else {
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("升级失败，摄像头版本号校验异常");
            return;
        }

    }

}
//选择摄像头固件
void Update::on_pushButton_16_clicked()
{
    m_choosefile[0] = false;
    QSettings setting("FileAddress.ini", QSettings::IniFormat);
    setting.setIniCodec(QTextCodec::codecForName("GB2312"));
    QString lastpath;
    lastpath = setting.value("File/LastFile").toString();
    filepath[0] = QFileDialog::getOpenFileName(this, "open file", lastpath, "text file(*.tsm);;All file(*.*)");
    if (!filepath[0].isEmpty()){
        setting.setValue("File/LastFile", filepath[0]);
    }

    localFile[0] = new QFile(filepath[0]);

    if (!localFile[0]->open((QFile::ReadOnly)))
    {
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]打开升级文件失败");
        return;
    }else{
        QByteArray msg;
        msg = localFile[0]->read(48);
        msg.remove(0, 16);

        if (ui.lineEdit_16->text().size() > 1)
        {
            msg.remove(0, 10);
            QString strData;
            strData = QString::fromLocal8Bit(msg.mid(0, 6).toHex().toUpper());
            if (strData != ui.lineEdit_16->text()){
                ui.textEdit->setTextColor(Qt::red);
                ui.textEdit->append("[上位机]摄像头升级文件不匹配");
                return;
            }
        }
        m_choosefile[0] = true;
        QFileInfo fileInfo(localFile[0]->fileName());
        ui.lineEdit_19->setStyleSheet("color:rgb(255,0,0)");
        ui.lineEdit_19->setText(fileInfo.fileName());
        ui.textEdit->setTextColor(Qt::darkGreen);
        ui.textEdit->append("[上位机]已选择摄像头升级文件：" + filepath[0]);
        localFile[0]->close();
        return;
    }
}

void Update::Slot_checkversiontimeout(){
    m_checkversiontimer->stop();
    if(checkversionnum < 2){
        m_socket->abort();
        m_socket->connectToHost(ui.lineEdit_6->text(),5000);
        checkversionnum++;
        m_checkversiontimer->start(8000);
    }else{
        m_socket->abort();
        checkversionnum = 0;
        name = "转接板";
        saveUpdatelog(false);
    }
}



void Update::Slot_showADBans(int type,QString msg){
    qDebug() << msg;
    switch(type){
    case 0:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]ADB连接雷达异常" + msg);
        if(adbThread->isRunning()){
            ui.textEdit->append("[上位机]请等待ADB操作终止再进行升级");
            adbThread->quit();
            //adbThread->wait();
        }

        break;
    case 1:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]ADB连接雷达完成，查询硬件特征号");
        break;
    case 2:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]ADB重连雷达异常" + msg);
        if(adbThread->isRunning()){
            ui.textEdit->append("[上位机]请等待ADB操作终止再进行升级");
            adbThread->quit();
            //adbThread->wait();
        }
        break;
    case 3:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]ADB连接雷达完成，查询硬件特征号");
        break;
    case 4:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]ADB查询硬件特征号异常" + msg);
        if(adbThread->isRunning()){
            ui.textEdit->append("[上位机]请等待ADB操作终止再进行升级");
            adbThread->quit();
            //adbThread->wait();
        }
        break;
    case 5:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]ADB查询硬件特征号完成" + msg);
        m_hardwarenum.clear();
        m_hardwarenum.append(msg);
        break;
    case 6:
        if(m_type){
            ui.textEdit->setTextColor(Qt::black);
            ui.textEdit->append("[上位机]ADB杀死现有雷达进程完成，准备删除旧版固件");
        }else{
            ui.textEdit->setTextColor(Qt::black);
            ui.textEdit->append("[上位机]ADB杀死现有摄像头进程完成，准备删除旧版固件");
        }

        break;
    case 7:
        if(m_type){
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("[上位机]ADB杀死现有雷达进程异常" + msg);
        }
        else{
            ui.textEdit->setTextColor(Qt::red);
            ui.textEdit->append("[上位机]ADB杀死现有摄像头进程异常" + msg);
        }
        if(adbThread->isRunning()){
            ui.textEdit->append("[上位机]请等待ADB操作终止再进行升级");
            adbThread->quit();
            //adbThread->wait();
        }
        break;
    case 8:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]ADB删除现有固件完成，准备下发新固件");
        break;
    case 9:
        ui.textEdit->setTextColor(Qt::red);
        if(m_type){
            ui.textEdit->append("[上位机]ADB删除现有雷达固件异常" + msg);
        }else{
            ui.textEdit->append("[上位机]ADB删除现有摄像头固件异常" + msg);
        }
        if(adbThread->isRunning()){
            ui.textEdit->append("[上位机]请等待ADB操作终止再进行升级");
            adbThread->quit();
            //adbThread->wait();
        }
        break;
    case 10:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]ADB下发新固件完成，准备授权");
        break;
    case 11:
        ui.textEdit->setTextColor(Qt::red);
        if(m_type){
            ui.textEdit->append("[上位机]ADB下发雷达新固件异常" + msg);
        }else{
            ui.textEdit->append("[上位机]ADB下发摄像头新固件异常" + msg);
        }
        if(adbThread->isRunning()){
            ui.textEdit->append("[上位机]请等待ADB操作终止再进行升级");
            adbThread->quit();
            //adbThread->wait();
        }
        break;
    case 12:
        ui.textEdit->setTextColor(Qt::black);
        ui.textEdit->append("[上位机]ADB下发新固件权限完成，准备重启雷达校验版本");
        break;
    case 13:
        ui.textEdit->setTextColor(Qt::red);
        if(m_type){
            ui.textEdit->append("[上位机]ADB下发雷达新固件权限异常" + msg);
        }else{
            ui.textEdit->append("[上位机]ADB下发摄像头新固件权限异常" + msg);
        }
        if(adbThread->isRunning()){
            ui.textEdit->append("[上位机]请等待ADB操作终止再进行升级");
            adbThread->quit();
            //adbThread->wait();
        }
        break;
    case 14:
        ui.textEdit->setTextColor(Qt::black);
        if(m_type){
            ui.textEdit->append("[上位机]ADB重启完成，等待30S后校验雷达版本");
        }else{
            ui.textEdit->append("[上位机]ADB重启完成，等待30S后后校验摄像头版本");
        }
        if(adbThread->isRunning()){
            adbThread->quit();
            //adbThread->wait();
        }
        m_readb->start(30000);
        break;
    case 15:
        ui.textEdit->setTextColor(Qt::red);
        ui.textEdit->append("[上位机]ADB重启异常" + msg);
        if(adbThread->isRunning()){
            adbThread->quit();
            ui.textEdit->append("[上位机]请等待ADB操作终止再进行升级");
            //adbThread->wait();
        }
        break;
    }
}

void Update::Slot_threadfinished(){
    ui.textEdit->setTextColor(Qt::blue);
    ui.textEdit->append("[上位机]ADB线程结束，等待校验版本或进行其余操作");
}

QByteArray Update::hexStrToByteArray(const QString &str)
{
    QByteArray senddata;
    int hexdata, lowhexdata;
    int hexdatalen = 0;
    int len = str.length();
    senddata.resize(len / 2);
    char lstr, hstr;

    for (int i = 0; i < len;) {
        hstr = str.at(i).toLatin1();
        if (hstr == ' ') {
            i++;
            continue;
        }

        i++;
        if (i >= len) {
            break;
        }

        lstr = str.at(i).toLatin1();
        hexdata = convertHexChar(hstr);
        lowhexdata = convertHexChar(lstr);

        if ((hexdata == 16) || (lowhexdata == 16)) {
            break;
        }
        else {
            hexdata = hexdata * 16 + lowhexdata;
        }

        i++;
        senddata[hexdatalen] = (char)hexdata;
        hexdatalen++;
    }

    senddata.resize(hexdatalen);
    return senddata;
}

char Update::convertHexChar(char ch)
{
    if ((ch >= '0') && (ch <= '9')) {
        return ch - 0x30;
    }
    else if ((ch >= 'A') && (ch <= 'F')) {
        return ch - 'A' + 10;
    }
    else if ((ch >= 'a') && (ch <= 'f')) {
        return ch - 'a' + 10;
    }
    else {
        return (-1);
    }
}
